#include "friendlist.h"
#include "ui_friendlist.h"
#include "changepersonalinfo.h"
#include "user.h"
#include <QHostAddress>
#include <QMessageBox>

friendlist::friendlist(QWidget *parent, user userSelf) :
    QWidget(parent),
    ui(new Ui::friendlist)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    this->setWindowTitle("好友列表");
    ui->textBrowser_name->append(userSelf.getUserName());
    ui->textBrowser_id->append(userSelf.getUserId());
    ui->textBrowser_gender->append(userSelf.getUserGender());
    ui->frame_photo->setStyleSheet("background-image:url("/*这里插入图片地址*/") ");
    //connect(ui->btn_getfriendlist,&QPushButton::clicked,this, &friendlist::btn_getfriendlist_clicked);
    //ui->messageSendTextEdit->clear();
    this->close();
    //向后端发送信息
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/add_friend"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");
    QJsonObject json2{
        {"id",userSelf.getUserId().toInt()},
        {"username",userSelf.getUserName()},
        {"token",userSelf.getUserPassword()}
    };
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    QString message;
    connect(reply, &QNetworkReply::finished, [=]()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 读取响应数据
            QByteArray dataReply = reply->readAll();
            // 将 JSON 数据转换为 QVariant 对象
            QJsonParseError error;
            QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
            //qDebug()<<result;
            // 处理 JSON 数据
            if(error.error == QJsonParseError::NoError)
            {
                QVariantMap resultMap = result.toVariantMap();

                //提取状态码
                QVariant status_code = resultMap.value("status_code");
                status_code.toString();
                QVariant sum_num = resultMap.value("sum_num");
                //sum_num.toString();
                QString message=resultMap.value("status_msg").toString();
                ui->textBrowser_friendlist->append("好友总数："+sum_num.toString());
                QJsonArray ucMsg = resultMap.value("uc_messages").toJsonArray();
                for(int i = 0; i < ucMsg.size(); i++)//循环添加好友列表（我不是很清楚是不是这么写）
                {
                    QJsonObject ucJsonObj = ucMsg.at(i).toObject();
                    QVariantMap messageMap = ucJsonObj.toVariantMap();
                    QMap friendMap = messageMap.value("friends").toMap();
                    QVariant friendName = friendMap.value("username");
                    QVariant friendId = friendMap.value("id");
                    QVariant friendGender = friendMap.value("gender");
                    //qDebug() << "sender信息111" << sender;
                    for(int i=1;i<=sum_num.toInt();i++)
                        ui->textBrowser_friendlist->append("第"+(QString)i+"个好友 id为："+friendId.toString()
                                                           +" 账号名称为："+friendName.toString()
                                                           +" 性别为："+friendGender.toString());
                    //ui->dialogTextBrowser->append("<font color = 'black' size = 24px>" + ucMsgContent + "<></font>");
                    //对话框中添加信息

                }
            }
        }
        else
            qDebug()<<"error";
        reply->deleteLater();
    });
}

friendlist::~friendlist()
{
    delete ui;
}

//修改个人信息
void friendlist::on_toolButton_infChange_clicked()
{
    this->close();
    changepersonalinfo *cpi=new changepersonalinfo(nullptr,userSelf);
    cpi->show();
}

